import request from "./request";
import qs from "qs";
import { popScopeId } from "vue";
// 封装登录接口
interface AdminLoginParams {
  password: string;
  username: string;
}

interface ResData<T> {
  errno: number;
  code: number;
  data: T;
  message: string;
}

interface AdminLoginResData {
  token: string;
  tokenHead: string;
}

type PromiseResData<T> = Promise<ResData<T>>;
// export const adminLogin = (
//   data: AdminLoginParams
// ): Promise<ResData<AdminLoginResData>> =>
//   request.post("http://120.24.64.5:8088/mall-admin/admin/login", data);
// 简写
export const adminLogin = (
  data: AdminLoginParams
): PromiseResData<AdminLoginResData> => request.post("/admin/login", data);

// 获取当前用户登录信息
interface adminInfoResData {
  menus: [];
  username: string;
}
export const getAdminInfo = (): PromiseResData<adminInfoResData> =>
  request.get("/admin/info");

// 获取用户列表
// 用户列表参数
interface AdminListParams {
  keyword: string;
  pageNum: number;
  pageSize: number;
}

// 用户列表返回值
interface AdminListRes {
  list: [];
  total: number;
}
export const getAdminList = (
  params: AdminListParams
): PromiseResData<AdminListRes> => request.get("/admin/list", { params });

// 更新指定用户信息
export const updateAdmin = (
  id: number,
  data: AdminDataObj
): PromiseResData<unknown> => request.post("/admin/update/" + id, data);

// 获取所有角色
export const getRoleListAll = (): PromiseResData<RoleObjRes[]> =>
  request.get("/role/listAll");

// 获取当前的角色
export const getRoleById = (adminId: number): PromiseResData<RoleObjRes[]> =>
  request.get("/admin/role/" + adminId);

// 给用户分配角色 / admin / role / update;

// 分配用户角色类型
interface UpdateAdminRoleParams {
  adminId: number;
  roleIds: string;
}
// post  请求使用query 传参
export const updateAdminRole = (
  data: UpdateAdminRoleParams
): PromiseResData<unknown> =>
  request.post("/admin/role/update/", null, { params: data });

// post  使用 form-data传参
export const updateAdminRole1 = (
  data: UpdateAdminRoleParams
): PromiseResData<unknown> =>
  request.post("/admin/role/update/", qs.stringify(data));

// 获取首页数据

// 首页图表数据返回类型
interface AdminIndexStatRes {
  salePie: SalePieType;
  saleMap: SalePieType;
}
export const getAdminStat = (): PromiseResData<AdminIndexStatRes> =>
  request.get("http://kumanxuan1.f3322.net:8360/admin/stat");

// 获取商品列表
interface productListResData {
  list: [];
  total: number;
}
interface productListParams {
  keyword: string;
  pageNum: number;
  pageSize: number;
}
export const productList = (
  params: productListParams
): PromiseResData<productListResData> =>
  request.get("/product/list", { params });

// sku商品库存管理
interface getSkuDataParams {
  keyword: string;
}

export const getSkuData = (
  pid: number,
  params: getSkuDataParams
): PromiseResData<getSkuDataRes[]> => request.get("/sku/" + pid, { params });

// 修改商品上架状态
interface updatePublishStatusParams {
  ids: number | number[];
  publishStatus: number;
}

export const updatePublishStatus = (
  data: updatePublishStatusParams
): PromiseResData<unknown> =>
  request.post("/product/update/publishStatus", null, { params: data });

// 删除商品接口
interface updateDelProductParams {
  deleteStatus: number;
  ids: number;
}
// post 使用query传参
export const updateDelProduct = (
  data: updateDelProductParams
): PromiseResData<unknown> =>
  request.post("/product/update/deleteStatus", null, { params: data });

// 获取商品类型
interface productAttributeListResData {
  list: [];
  total: number;
}

interface productAttributeListParams {
  pageNum: number;
  pageSize: number;
}

export const productAttributeList = (
  params: productAttributeListParams
): PromiseResData<productAttributeListResData> =>
  request.get("/productAttribute/category/list", { params });

// 删除单个商品类型
export const productAttributeDel = (
  id: number
): PromiseResData<productAttributeListResData> =>
  request.get("/productAttribute/category/delete/" + id);

// 获取属性列表和参数列表
interface getAttrListResData {
  list: [];
  total: number;
}

interface getAttrListParams {
  pageNum: number;
  pageSize: number;
  type: number;
}

export const getAttrList = (
  cid: number,
  params: getAttrListParams
): PromiseResData<getAttrListResData> =>
  request.get("/productAttribute/list/" + cid, { params });

// 删除商品属性信息 批量 /productAttribute/delete
interface updateDelAttributeParams {
  ids: number[] | number;
}
// post 使用query传参
export const updateDelAttribute = (
  data: updateDelAttributeParams
): PromiseResData<unknown> => {
  return request.post("/productAttribute/delete", qs.stringify(data));
};

// 获取商品品牌列表
interface getBrandListResData {
  list: [];
  total: number;
}

interface getBrandListParams {
  keyword: string;
  pageNum: number;
  pageSize: number;
}

export const getBrandList = (
  params: getBrandListParams
): PromiseResData<getBrandListResData> =>
  request.get("/brand/list", { params });

// 删除单个品牌
export const getBrandDEL = (id: number): PromiseResData<getBrandListResData> =>
  request.get("/brand/delete/" + id);

// 更新厂家制造商
interface updateFactoryStatusParams {
  ids: number[] | number;
  factoryStatus: number;
}
// post 使用query传参
export const updateFactoryStatus = (
  data: updateFactoryStatusParams
): PromiseResData<unknown> => {
  return request.post("/brand/update/factoryStatus", qs.stringify(data));
};

// 更新显示状态
interface updateShowStatusParams {
  ids: number[] | number;
  showStatus: number;
}
// post 使用query传参
export const updateShowStatus = (
  data: updateShowStatusParams
): PromiseResData<unknown> => {
  return request.post("/brand/update/showStatus", qs.stringify(data));
};

// 获取商品分类数据 ！！！！
interface categoryListResData {
  list: [];
  total: number;
}

interface categoryListParams {
  pageNum: number;
  pageSize: number;
}

export const categoryList = (
  parentId: number,
  params: categoryListParams
): PromiseResData<categoryListResData> =>
  request.get("/productCategory/list/" + parentId, { params });

// 删除商品分类  /{id}
export const categoryDelete = (id: number): PromiseResData<unknown> =>
  request.post("/productCategory/delete/" + id);
